#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
//滑动窗口，同向双指针 
//长度最小的子数组
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int left = 0;
        int right = 0;
        int n = nums.size();
        int sum = nums[left], len = 0;
        while (left < n && right < n)
        {
            while (left <= right && sum >= target)
            {
                if (left < n && right < n) len = right - left + 1;
                sum -= nums[left];
                left++;
            }
            right++;
            if (right < n) sum += nums[right];
        }
        return len;

    }
};
//调试后的正确代码，首先不用麻烦的确定left一定是小于right的，其次注意取代的思路是错误的后面有可能取代的值比之前大
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int left = 0;
        int right = 0;
        int n = nums.size();
        int sum = 0, len = INT_MAX;
        while (right < n)
        {
            sum += nums[right];

            while (sum >= target)
            {
                len = min(len, right - left + 1);
                sum -= nums[left];
                left++;
            }
            right++;

        }
        return len == INT_MAX ? 0 : len;

    }
};


//无重复字符的最长子串
class Solution {
public:
    int lengthOfLongestSubstring(string s)
    {
        int n = s.size();
        int ret = 0;
        int hash[128] = { 0 };
        for (int left = 0, right = 0; right < n; right++)
        {
            hash[s[right]]++;
            while (hash[s[right]] > 1)
            {
                hash[s[left++]]--;
            }
            ret = max(ret, right - left + 1);
        }
        return ret;
    }
};

int main()
{
    vector<int> vv = { 5,1,3,5,10,7,4,9,2,8 };
    int ans = Solution().minSubArrayLen(7, vv);
    cout << ans << endl;

	return 0;
}